<template>
  <div>
    <div class="operate">
      <el-button type="primary" size="small" class="operate-item" @click="saveRoleVisible=true">新增</el-button>
    </div>

    <div class="data" v-loading="loading">
      <el-table  :data="data.records">
        <el-table-column prop="name" label="名称" width="150"> </el-table-column>
        <el-table-column prop="levelName" label="等级" width="100"> </el-table-column>
        <el-table-column prop="createTime" label="创建时间" min-width="170" width="auto"> </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          width="150">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="showUpdate(scope.row.id)">编辑</el-button>
            <el-popconfirm title="确定要删除吗？" @onConfirm="remove(scope.row.id)">
              <el-button style="margin-left: 10px;" type="danger" size="mini" slot="reference">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="current"
        :page-sizes="$config.defaultPageSizeOpts"
        :page-size="size"
        layout="total, sizes, prev, pager, next"
        :total="data.total">
      </el-pagination>
    </div>

    <SaveRole :visible="saveRoleVisible"
              @on-close="saveRoleVisible=false"
              @on-success="saveSuccess">
    </SaveRole>
    <UpdateRole :visible="updateRoleVisible"
                :id="id"
                @on-close="updateRoleVisible=false"
                @on-success="updateSuccess">
    </UpdateRole>
  </div>
</template>
<script>
import { mapActions } from 'vuex'
import DictSelect from '@/components/form/DictSelect'
import SaveRole from '@/views/system/role/SaveRole'
import UpdateRole from '@/views/system/role/UpdateRole'

export default {
  name: 'Role',
  components: { UpdateRole, SaveRole, DictSelect },
  data() {
    return {
      data: {},
      params: {},
      form: {},
      current: 1,
      size: 10,
      saveRoleVisible: false,
      updateRoleVisible: false,
      id: '',
      loading: false
    }
  },
  methods: {
    ...mapActions(['GetRolePage', 'RemoveRoleById']),
    search() {
      this.params.current = this.current
      this.params.size = this.size
      this.loading = true
      this.GetRolePage(this.params).then(res => {
        this.loading = false
        if (res) {
          this.data = res
        }
      }).catch(() => {
        this.loading = false
      })
    },
    reset() {
      this.params = {}
      this.current = 1
    },
    searchByCondition() {
      this.current = 1
      this.search()
    },
    handleSizeChange(val) {
      this.size = val
      this.search()
    },
    handleCurrentChange(val) {
      this.current = val
      this.search()
    },
    saveSuccess() {
      this.search()
      this.saveRoleVisible = false
    },
    showUpdate(id) {
      this.id = id
      this.updateRoleVisible = true
    },
    updateSuccess() {
      this.search()
      this.updateRoleVisible = false
    },
    remove(id) {
      this.RemoveRoleById(id).then(() => {
        this.search()
      })
    }
  },
  mounted() {
    this.search()
  }
}
</script>

<style lang="less" scoped>
  .search {
    .search-item {
      width: 200px;
    }
  }
  .operate {
    float: right;
    operate-item {
      padding: 0 10px;
    }
  }
</style>
